package com.longg.dao;

import com.longg.comm.Comm;
import com.longg.dao.base.BaseDao;
import com.longg.model.User;
import com.longg.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;


public class UserDao extends BaseDao<User> {

    public UserDao() {
        super(User.class);
    }

    public Map<String, Object> getUserBaseInfo(String username) {
        String sql = "select user.id,user.username,ui.real_name as realName,ui.pic,dept.name \n" +
                "from user left join user_info ui on user.id = ui.user_id\n" +
                "left join dept\n" +
                "on user.dept_id = dept.id\n" +
                "where user.username = ?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        Map<String,Object> userMap = null;
        try {
            userMap = queryRunner.query(sql,new MapHandler(),username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userMap;
    }

    public Map<String,Object> getUserInfo(int id){
        String sql = "select u.id,u.is_secret,ui.*,dept.`name` as dept_name,(select count(user_id) from user_focus where user_focus_id = u.id) as focusNum\n" +
                "from user u left join user_info ui on u.id = ui.user_id\n" +
                "left join dept on u.dept_id = dept.id\n" +
                "where u.id = ?";
        Map<String,Object> infoMap = null;
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            infoMap = queryRunner.query(sql,new MapHandler(),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return infoMap;
    }

    public List<Map<String, Object>> findUsersByPage(int page,int id,String name) {
        String where = "";
        if (null != name && !"".equals(name)){
            where = " where real_name like ? ";
        }
        String sql = "select ui.user_id,ui.real_name,ui.gender,ui.age,ui.interduce,(select id from user_focus where user_id = ? and user_focus_id = ui.user_id) as fid \n" +
                "from user_info ui\n" +
                where +
                "limit ?,?";

        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> query = null;
        try {
            if (null != name && !"".equals(name)){
                query = queryRunner.query(sql, new MapListHandler(), id, "%" + name + "%",(page - 1) * Comm.PAGE_SIZE, Comm.PAGE_SIZE);
            }else{
                query = queryRunner.query(sql, new MapListHandler(), id, (page - 1) * Comm.PAGE_SIZE, Comm.PAGE_SIZE);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }


}
